package com.example.spring5.JdbcTemplateDemo;

import lombok.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 1、什么是 JdbcTemplate
 * （1）Spring 框架对 JDBC 进行封装，使用 JdbcTemplate 方便实现对数据库操作
 * 2、准备工作
 *  （1）引入相关 jar 包
 *  （2）在 spring 配置文件配置数据库连接池          JdbcTemplate.xml 里
 *  （3）配置 JdbcTemplate 对象，注入 DataSource   JdbcTemplate.xml 里
 *  （4）创建 service 类，创建 dao 类，在 dao 注入  jdbcTemplate 对象
 *
 * JavaEE
 *    WEB层      接口 和前端连接
 *    Service层  业务操作    事务一般加在这
 *    Dao层      数据库操作
 *    见 JdbcTemplateDemo这个包
 *
 *
 *
 */
public class 概述 {
    static ApplicationContext context = new ClassPathXmlApplicationContext("JdbcTemplate.xml");

    public static void main(String[] args) {
        UserLongService userLongService = context.getBean("userLongService", UserLongService.class);

        UserLong userLong = new UserLong(5L,"张三","666");
        userLongService.addUserLong(userLong);
//        System.out.println(userLongService.selectCount());
//        System.out.println(userLongService.selectUserLongList());
//        System.out.println(userLongService.selectUserLong(5L));

    }
}
@Service
class UserLongService{
    @Autowired
    UserLongDao userLongDao;

    public void addUserLong(UserLong userLong){
        userLongDao.addUserLong(userLong);
    }
    public void deleteUserLong(Long id){
        userLongDao.deleteUserLong(id);
    }
    public void updateUserLong(UserLong userLong){
        userLongDao.updateUserLong(userLong);
    }
    public UserLong selectUserLong(Long id){
        return userLongDao.selectUserLong(id);
    }
    public List<UserLong> selectUserLongList(){
        return userLongDao.selectUserLongList();
    }
    public Long selectCount(){
        return userLongDao.selectCount();
    }

}
interface UserLongDao{
    void addUserLong(UserLong userLong);
    void deleteUserLong(Long id);
    void updateUserLong(UserLong userLong);
    UserLong selectUserLong(Long id);
    List<UserLong> selectUserLongList();
    Long selectCount();


}
@Repository
class UserLongDaoImpl implements UserLongDao{
    @Autowired
    JdbcTemplate jdbcTemplate;

    public void 其他操作(UserLong userLong) {
        String sql = "insert into user_long values(?,?,?)";//批量添加
        String sql1 = "update user_long set username=?,password=? where id=?";//批量修改

        jdbcTemplate.batchUpdate(sql, new ArrayList<Object[]>());//同理批量

    }

    @Override
    public void addUserLong(UserLong userLong) {
        Object[] args = {userLong.getId(), userLong.getUsername(),
                userLong.getPassword()};

//        jdbcTemplate.update("insert into user_long values (?,?,?)",
//               userLong.getId(),userLong.getUsername(),userLong.getPassword());

        jdbcTemplate.update("insert into user_long values (?,?,?)",
                args);
    }

    @Override
    public void deleteUserLong(Long id) {
        jdbcTemplate.update("delete from user_long where id=?",id);
    }

    @Override
    public void updateUserLong(UserLong userLong) {
        String sql = "update user_long set username=?,password=? where id=?";
        Object[] args = {userLong.getUsername(), userLong.getPassword(),userLong.getId()};
        int update = jdbcTemplate.update(sql, args);
        System.out.println(update);
    }

    @Override
    public UserLong selectUserLong(Long id) {
        String sql = "select * from user_long where id=?";
        UserLong list = jdbcTemplate.queryForObject(
                sql,new BeanPropertyRowMapper<>(UserLong.class),id);
        return list;
    }

    @Override
    public List<UserLong> selectUserLongList() {
        String sql = "select * from user_long";
        List<UserLong> list = jdbcTemplate.query(
                sql, new BeanPropertyRowMapper<>(UserLong.class));
        return list;
    }

    @Override
    public Long selectCount() {
        String sql = "select count(*) from user_long";
        Long aLong = jdbcTemplate.queryForObject(sql, Long.class);
        return aLong;
    }

}
@ToString//toString方法
@Data//get set方法
@NoArgsConstructor//无参构造器
@AllArgsConstructor//全参构造器
@EqualsAndHashCode//EqualsAndHashCode方法
class UserLong {
    private Long id;
    private String username;
    private String password;

    public UserLong(String username, String password) {
        this.username = username;
        this.password = password;
    }
}


